/*
 * @Copyright: 2025 Copyright (C) AMCAX, All rights reserved.
 * @Author: Zhang
 * @Date: 2025/7/24 19:39
 * @LastEditTime: 2025/7/24 19:39
 * @LastEditors: Zhang
 * @Description:
 */
import { RenderBase } from '@/common/cmdBase/renderBase';
import { widgetManager } from '@/common/managers';
import { createAxis } from '@/common/utils';
import { surfEvaluate } from '@/domain';
import { MessageTip } from '@amcax/base';
import { THREE } from '@amcax/web-render';
import { CmdDialogForm } from 'nextcae-base';
import { useEffect, useRef, useState } from 'react';
import { NMMeshBaseCommand, TopoDialogProps } from '../type';
import { schema, uiSchema } from './data';
import './index.scss';

const PointCloudClearDialog = ({ title, onClose }: TopoDialogProps) => {
  const [formData, setFormData] = useState<any>({
    offDir: [0, 0, 0],
    refOrigin: [0, 0, 0],
    refAglDir: [0, 0, 0],
  });
  // 渲染、拾取相关的功能类
  const renderBase = useRef<RenderBase>(new RenderBase());

  // 组件退出，清理数据
  useEffect(() => {
    return () => {
      renderBase.current.clearAssisMgr();
    };
  }, []);
  useEffect(() => {
    renderBase.current.assisMgr.group.clear();
    // 判断RefDir、RefAglDir、RefOrigin的x/y/z是否为null
    const { offDir, refOrigin, refAglDir } = formData;
    if (
      offDir.length === 0 ||
      refOrigin.length === 0 ||
      refAglDir.length === 0
    ) {
      return;
    }

    // 计算模型的包围盒，用来设置线、平面的大小
    const meshMgr = renderBase.current.meshMgr;
    const box = new THREE.Box3().setFromObject(meshMgr.group);

    // 根据轴向方向和轴向起点创建基准轴
    const OffDir = new THREE.Vector3(offDir[0], offDir[1], offDir[2]);
    const RefOrigin = new THREE.Vector3(
      refOrigin[0],
      refOrigin[1],
      refOrigin[2],
    );
    const size = box.getSize(new THREE.Vector3());
    const length = Math.max(size.x, size.y, size.z);
    const arrow = createAxis(
      OffDir,
      RefOrigin,
      0xff6b6b,
      length * 1.2,
      2,
      length / 10,
      length / 20,
    );
    renderBase.current.assisMgr.group.add(arrow);

    renderBase.current.render();
  }, [formData]);

  useEffect(() => {
    widgetManager.hint('进入脱模检查命令!');
    return () => {
      widgetManager.hint();
    };
  }, []);

  const onSubmit = async () => {
    const { Selection, offDir, refOrigin, refAglDir } = formData;
    const params = {
      surfaces: Selection?.source?.map((item) => item.dxid),
      offDir: [offDir[0], offDir[1], offDir[2]],
      refOrigin: [refOrigin[0], refOrigin[1], refOrigin[2]],
      refAglDir: [refAglDir[0], refAglDir[1], refAglDir[2]],
    };
    const req = await surfEvaluate.removalShape(params);
    if (req?.demold) {
      MessageTip.success('脱模检测成功!');
    } else {
      MessageTip.warning('脱模检测失败，请检查模型!');
    }
    onClose();
  };
  return (
    <>
      <CmdDialogForm
        title={title}
        titleIcon={'commands/surfaceAnalysis/removal-shape'}
        schema={schema}
        extraUiSchema={uiSchema}
        formData={formData}
        fields={{}}
        onChange={(data) => {
          setFormData(data.formData);
        }}
        onSubmit={onSubmit}
        onClose={onClose}
        isFormDataValid={
          formData?.Selection?.source?.length !== 0 ||
          formData?.offDir.length !== 0 ||
          formData?.refOrigin.length !== 0 ||
          formData?.refAglDir.length !== 0
        }
        width='300px'
        formClassName='structure-form'
      />
    </>
  );
};
export const cmdRemovalShape: NMMeshBaseCommand = {
  id: 'cmdRemovalShape',
  icon: 'commands/surfaceAnalysis/removal-shape',
  label: '脱模检查',
  create: (label: string, onClose: () => void, props) => (
    <PointCloudClearDialog
      title={label}
      onClose={onClose}
      props={props}
      placement='topLeft'
    />
  ),
};
